const handelDomicile = (item, valueType) => {
  if (item[valueType]?.city) {
    return (
      (item[valueType]?.province ?? '') +
      '-' +
      (item[valueType]?.city ?? '') +
      '-' +
      (item[valueType]?.name ?? '')
    )
  } else {
    return item[valueType]?.province ?? ''
      ? (item[valueType]?.province ?? '') + '-' + (item[valueType]?.name ?? '')
      : ''
  }
}
function screenFiledMsg(item, type, valueType) {
  const field = item?.field?.field
  switch (field) {
    case 'education_id':
    case 'marital_id':
    case 'applied_department':
    case 'applied_position':
    case 'politic_id':
    case 'room_id':
    case 'department_id':
    case 'position_id':
      return item[valueType]?.name ?? ''
    case 'gender':
      return item[type] === '0' ? '女' : item[type] === '1' ? '男' : ''
    case 'domicile_id':
      return handelDomicile(item, valueType) ?? ''
    case 'nation_id':
      return item[valueType]?.nation ?? ''
    case 'certificate':
      return item[type] === '0' ? '无' : item[type] === '1' ? '有' : ''
    case 'introducer_id':
    case 'leader_id':
      return item[valueType]?.username ?? ''
    case 'identity':
      return item[type] === '0'
        ? '正式员工'
        : item[type] === '1'
          ? '实习员工'
          : '试用员工'
    case 'job_status':
      return item[type] === '0' ? '离职' : item[type] === '1' ? '在职' : ''
    case 'registered_residence':
      return item[type] === '0' ? '城市' : item[type] === '1' ? '农村' : ''
    case 'education_nature':
      return item[type] === '0'
        ? '自考'
        : item[type] === '1'
          ? '成人教育'
          : item[type] === '2'
            ? '统招'
            : ''
    case 'school_id':
      return item[valueType]?.school_name ?? ''
    case 'bank_id':
      return item[valueType]?.bankName ?? ''
    default:
      return item[type] ?? ''
  }
}
const contractData = [
  'startYear',
  'startMonth',
  'startDay'
]
const contractEndData = [
  'endYear',
  'endMonth',
  'endDay'
]
let start = 'yyyy-mm-dd'
const handelLaborStart = (item, type) => {
  if (item?.field?.field === 'startYear') {
    start = start.replace('yyyy', item[type])
  } else if (item?.field?.field === 'startMonth') {
    start = start.replace('mm', item[type])
  } else if (item?.field?.field === 'startDay') {
    start = start.replace('dd', item[type])
  }

  if (!/[ymd]/i.test(start)) {
    	return { label: '劳动合同开始时间', value: start }
  	}
}
let endContract = 'yyyy-mm-dd'
const handelLaborEnd = (item, type) => {
  if (item?.field?.field === 'endYear') {
    endContract = endContract.replace('yyyy', item[type])
  } else if (item?.field?.field === 'endMonth') {
    endContract = endContract.replace('mm', item[type])
  } else if (item?.field?.field === 'endDay') {
    endContract = endContract.replace('dd', item[type])
  }
  if (!/[ymd]/i.test(endContract)) {
    	return { label: '劳动合同结束时间', value: endContract }
  	}
}
const isLookTable = [
  'certificate_list',
  'work_experience',
  'education_experience',
  'emergency_contact',
  'family_situation'
]
export const handleFiledData = (arr, type, valueType) => {
  if (Array.isArray(arr) && arr.length > 0) {
    const userInfoData = {
      handleArr: [],
      experienceArr: []
    }
    userInfoData.handleArr = arr
      ?.filter(
        (filed) =>
          filed?.field?.field !== 'open_id' &&
          !isLookTable.includes(filed?.field?.field)
      )
      .map((item) => {
        if (item?.field?.type === 'file') {
          return {
            label: item.field?.name,
            value: '点击查看',
            url: item[type]
          }
        } else if (contractData.includes(item?.field?.field)) {
          return handelLaborStart(item, type)
        } else if (contractEndData.includes(item?.field?.field)) {
          return handelLaborEnd(item, type)
        } else {
          return {
            label: item.field?.name,
            value: screenFiledMsg(item, type, valueType)
          }
        }
      })
      .filter((item, index, self) => {
        return (
          item?.label !== undefined &&
          index ===
            self.findIndex(
              (t) => t?.label === item?.label && t?.value === item?.value &&
			  t?.url === item?.url
            )
        )
      })

    userInfoData.experienceArr = arr
      ?.filter((filed) => filed?.field?.field !== 'open_id')
      .map((item) => {
        if (isLookTable.includes(item?.field?.field)) {
          if (item[type]) {
            return {
              label: item.field?.name,
              value: JSON.parse(item[type])
            }
          } else {
            return {
              label: item.field?.name,
              value: ''
            }
          }
        }
      })
      .filter((item, index, self) => {
        return (
          item?.label !== undefined &&
          index ===
            self.findIndex(
              (t) => t?.label === item?.label && t?.value === item?.value &&
			  t?.url === item?.url
            )
        )
      })
    return userInfoData
  } else {
    console.log(`${arr} is not Array or ${arr} is lenght <=0`)
  }
}

/**
 *
 * @param {要处理的数组} arr
 * @param {返回的数据字段} type
 * @param {要查找的数据字段} name
 */
export const handleFiledByID = (arr, type, name) => {
  let filedMsg = null
  if (Array.isArray(arr) && arr.length > 0) {
    filedMsg = arr?.filter((filed) => filed?.field?.field === name)
    return filedMsg.length > 0 ? filedMsg[0][type] : ''
  } else {
    console.log(`${arr} is not Array or ${arr} is lenght <=0`)
  }
}

/**
 *
 * @param {要处理的数据} data
 * @param {field数组} fields
 * @returns
 */
export const mapFields = (data, fields) => {
  for (const key in data) {
    for (let i = 0; i < fields.length; i++) {
      if (fields[i].field === key) {
        data[fields[i].id] = data[key]
        delete data[key]
        break
      }
    }
  }
  return data
}

/**
 * @param {判断列表} map
 * @param {筛选条件} keys
 */

export function switchToObject(map, ...keys) {
  let result = map

  for (const key of keys) {
    if (Object.prototype.hasOwnProperty.call(result, key)) {
      result = result[key]
    }
  }

  return result
}
